import 'dart:async';

import 'package:flutter/material.dart';

class Swiper extends StatefulWidget {
  double width;
  double height;
  List<String> list;
  Swiper({super.key,this.width=double.infinity,this.height=200,required this.list});

  @override
  State<Swiper> createState() => _SwiperState();
}

class _SwiperState extends State<Swiper> {

  int _currentIndex = 0;
  final List<Widget> _pageList =[];
  late PageController _pageController;
  late Timer timer;

  @override
  void initState() {
    super.initState();
    _pageController = PageController(initialPage: 0);
    timer = Timer.periodic(const Duration(seconds: 3), (timer) {
        _pageController.animateToPage(
            (_currentIndex + 1) % _pageList.length,
            duration: const Duration(microseconds: 200),
            curve: Curves.linear
        );
    });
    for(var i = 0; i < widget.list.length; i++){
      _pageList.add(Image(image: NetworkImage(widget.list[i]),fit: BoxFit.cover));
    }
  }


  @override
  void dispose() {
    super.dispose();
    timer.cancel();
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        PageView.builder(
          scrollDirection: Axis.horizontal,
          controller: _pageController,
          itemCount: 10000,
          itemBuilder: (context, index) {
            return _pageList[_currentIndex];
          },
          onPageChanged: (index){
            setState(() {
              _currentIndex = index % _pageList.length;
            });
          },
        ),
        Positioned(
            left: 0,
            right: 0,
            bottom: 2,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: List.generate(3, (index) {
                return Container(
                  height: 10,
                  width: 10,
                  margin: const EdgeInsets.all(5),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: _currentIndex == index ? Colors.blue : Colors.grey,
                  ),
                );
              }),
            )),
      ],
    );
  }
}

